package com.oo.fileexplorer

import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import com.oo.fileexplorer.tools.FileResultStarck
import com.oo.fileexplorer.tools.FileScanner

class MainActivity : AppCompatActivity() {


    val fileStarck = FileResultStarck()
    var datalist = FileScanner.scanDisk()
    var recyclerView: RecyclerView? = null
    var adapter: MyRecyclerAdapter? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)


        recyclerView = findViewById<RecyclerView>(R.id.recycler_view)

        adapter = MyRecyclerAdapter(datalist, this)


        val layoutManager = LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);

        recyclerView?.layoutManager = layoutManager;
        recyclerView?.adapter = adapter;

        setlistener()
    }

    private fun setlistener() {
        adapter?.setItemClicklistener { position, filename ->
            //将已有结果压入栈
            fileStarck.addTop(datalist)
            datalist = FileScanner.scanDirectory(filename)
            adapter = MyRecyclerAdapter(datalist, this@MainActivity)
            recyclerView?.adapter = adapter

        }
    }


    /**
     * 用户点击返回键 判断 路径栈是否为空为空 执行 super 不为空退栈
     * */
    override fun onBackPressed() {
        val backList = fileStarck.pushTop()
        if (backList != null) {
            datalist = backList;
            adapter = MyRecyclerAdapter(datalist, this@MainActivity);
            recyclerView?.adapter = adapter
            setlistener()

        } else {
            super.onBackPressed()
        }

    }
}
